package panel.scenicShop
{
	import assists.ButtonUtils;
	import assists.HtmlText;
	import assists.Lang;
	import assists.SizeManager;
	import assists.URI;
	import assists.resource.ResourceLoader;
	
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.geom.Rectangle;
	
	import inerface.IScenicShopPanel;
	
	import main.ClientConstants;
	
//	import panel.eating.DragContainerBox;
	import panel.eating.EatingRestaurantBox;
	import panel.eating.GetMoreBox;
	import panel.eating.NiMaContainer;
	import panel.eating.RankBtnGroupList;
	import panel.publicPanel.NavigationBar;
	import panel.viewSpots.StaticSlide;
	
	import struct.TourElementBase;
	
	public class ScenicShopPanel extends Sprite implements IScenicShopPanel
	{
		
		private var _stage:Stage;
		private var _resourceLoader:ResourceLoader;
		private var _lang:Lang;
		private var _uri:URI;
		
		private var _shopBtnList:Array;
		private var container:NiMaContainer;
//		private var dragContainer:DragContainerBox;
		
		private var _onshowDetail:Function;
		private var _onShowLocation:Function;
		private var _guideBtns:NavigationBar;
		
		private var _onGetMore:Function;
		
		public function ScenicShopPanel()
		{
			_shopBtnList = [];
		}
		
		public function get content():Sprite
		{
			return this;
		}
		
		public function init(stage:Stage,resourceLoader:ResourceLoader,lang:Lang,uri:URI = null):void
		{
			_stage = stage;
			_resourceLoader = resourceLoader;
			_lang = lang;	
			_uri = uri;
			
			_guideBtns = new NavigationBar(_resourceLoader,_stage);
			_guideBtns.show([{label:"好评度",func:changeRankListByComment},{label:"距离",func:changeRankListByDistance},{label:"消费度",func:changeRankListByCosts}]);
			_guideBtns.y = SizeManager.guideUpHeight;
			
//			dragContainer = new DragContainerBox(_stage.stageWidth,_stage.stageHeight - _guideBtns.height - _guideBtns.y,_stage,new GetMoreBox(_stage,_resourceLoader,_lang,getMoreInfo));           //拖动对象添加容器
//			dragContainer.x = 0;
//			dragContainer.y = _guideBtns.y + _guideBtns.height;
//			this.addChild(dragContainer);
			
			container = new NiMaContainer(_stage,_resourceLoader,_lang,_uri);
			container.x = 0;
			container.y = _guideBtns.y + _guideBtns.height;
			this.addChild(container);
			
			this.addChild(_guideBtns);
		}
		
		private function changeRankListByComment():void
		{
			_shopBtnList.sortOn("outStarNum",Array.NUMERIC|Array.DESCENDING);
//			dragContainer.changeData(_shopBtnList);
			container.changeData(_shopBtnList);
		}
		
		private function changeRankListByDistance():void
		{
			_shopBtnList.sortOn("outDistance",Array.NUMERIC);
//			dragContainer.changeData(_shopBtnList);
			container.changeData(_shopBtnList);
		}
		
		private function changeRankListByCosts():void
		{
			_shopBtnList.sortOn("outDistance",Array.NUMERIC);
//			dragContainer.changeData(_shopBtnList);
			container.changeData(_shopBtnList);
		}
		
		private function changeRankListByMore():void
		{
			
		}
		
		public function changeData(list:Array):void
		{
			changeBoxData(list);
//			dragContainer.changeData(_shopBtnList);
		}
		
		private function changeBoxData(list:Array):void
		{
			if(_shopBtnList[_shopBtnList.length - 1] && _shopBtnList[_shopBtnList.length - 1] is GetMoreBox){
				_shopBtnList.pop();
			}
			
			var num:int = 0;
			num = _shopBtnList.length;
			
			for(var i:int = num;i < list.length;i++){
				
				var box:EatingRestaurantBox;
				box = new EatingRestaurantBox(_resourceLoader,_lang,_stage.stageWidth,_stage.stageHeight * .15,_uri);
				box.showDetail = showDetail;
				box.clearRecord = clearRecord;
				box.mapGuide = showMapLocation;
				_shopBtnList.push(box);
				
				box.changeData(list[i]);
			}
			
			if(list.length % 10 == 0 && list.length != 0){
				_shopBtnList.push(new GetMoreBox(_stage,_resourceLoader,_lang,_onGetMore));
			}
			
			container.changeData(_shopBtnList);
			StaticSlide.clear(container);
			
			if(container.y > 0)
			{
				StaticSlide.beginSlide(container,_stage,new Rectangle(0,container.y ,0, - container.height+ SizeManager.guideHeight + _guideBtns.height),container.y);
			}else
			{
				StaticSlide.beginSlide(container,_stage,new Rectangle(0,- container.height + _stage.stageHeight - SizeManager.guideDownHeight - SizeManager.guideUpHeight ,0,  container.height - _stage.stageHeight + SizeManager.guideHeight +  _guideBtns.height + SizeManager.guideUpHeight),_guideBtns.y + _guideBtns.height);	
				
			}
		}
		
		private function showDetail(data:TourElementBase):void
		{
			if(_onshowDetail is Function){
				_onshowDetail(data);
			}
		}
		
		private function clearRecord():void
		{
			for(var i:int = 0;i <_shopBtnList.length;i++){
				if(_shopBtnList[i] is EatingRestaurantBox){
					_shopBtnList[i].clearStart();
				}
			}	
		}
		
		private function showMapLocation(obj:*):void
		{
			if(_onShowLocation is Function){
				_onShowLocation(obj);
			}	
		}
		
		private function getMoreInfo():void
		{
			
		}
		
		public function set changeLocation(loc:Array):void
		{
			for(var i:int = 0;i < _shopBtnList.length;i++){
				_shopBtnList[i].currentLoaction = loc;
			}
		}
		
		public function set onShowDetail(func:Function):void
		{
			_onshowDetail = func;
		}
		
		
		public function set showLocation(func:Function):void
		{
			_onShowLocation = func;
		}
		
		public function set clickTurnPage(func:Function):void
		{
			_onGetMore = func;
		}
		
		public function clear():void
		{
			for(var i:int = 0;i < _shopBtnList.length;i++){
				_shopBtnList[i].clearThis();
				_shopBtnList[i] = null;
			}
			
			_shopBtnList = [];
//			dragContainer.clear();
//			dragContainer = null;
			
			_stage = null;
			_resourceLoader = null;
			_lang = null;
		}
		
	}
}